using System;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;
using Castle.MonoRail.Framework;
using Castle.MonoRail.Framework.Helpers;
using Castle.MonoRail.Framework.Adapters;
using NHibernate;
using NHibernate.Expression;
using VS.Facade;
using VS.Model;
using VS.Web.Helper;

namespace VS.Web.Controllers
{
    [Layout("default"), Rescue("generalerror"), Helper(typeof(VKShopPaginationHelper))]
    public class ProductController : SmartDispatcherController
    {
        public void Index()
        {

        }

        public void List(string id)
        {
            PropertyBag["PicSerUrl"] = "http://localhost:3960/Content/Upload/ProImg/";
            PropertyBag["PicType"] = "_s.jpg";
            VS_PType currPType = VS_PType.Find(int.Parse(id));
            bool hasChild = false;
            IList PTypes = PTypeSystem.Instance.GetPType(1, int.Parse(id));
            PropertyBag["PTypes"] = PTypes;
            if (PTypes.Count > 0)
            {
                hasChild = true;
            }
            PropertyBag["hasChild"] = hasChild;
            PropertyBag["currPType"] = currPType;

            IList Products = currPType.VS_Products;
            PropertyBag["Products"] = PaginationHelper.CreatePagination(this, Products, 20);
        }

        public void Info(string id)
        {
            PropertyBag["PicSerUrl"] = "http://localhost:3960/Content/Upload/ProImg/";
            PropertyBag["PicType"] = "_b.jpg";
            VS_Product product = VS_Product.Find(int.Parse(id));
            product.Hits++;
            product.Update();
            string strTemplate = "<option value={0}>{0}</option>";
            string[] arrSpecification = product.Specification.Split(' ');
            StringBuilder sb = new StringBuilder();
            foreach (string str in arrSpecification) {
                sb.AppendLine(string.Format(strTemplate, str));
            }
            PropertyBag["strSpecification"] = sb.ToString();
            PropertyBag["Product"] = product;
        }
    }
}
